package com.app.web.front.controller;

import java.io.File;
import java.util.UUID;

import com.app.util.AjaxResult;
import com.app.util.Const;
import com.app.util.DateUtils;
import com.app.util.FileUtils;
import com.app.web.BaseController;
import com.app.web.front.model.Blog;
import com.jfinal.kit.FileKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class BlogController extends BaseController{
	
	public void list(){
		
	}
	
	/**
	 * 根据 blog id 查找
	 */
	public void findById(){
				
		AjaxResult result = new AjaxResult(1);
		int blogId = this.getParaToInt("blog_id");
		Blog blog = Blog.dao.findById(blogId);
		Record blog0 = new Record();
		
		if(blog != null){
			blog0 = blog.getRecord();
		}
		boolean edit = this.getParaToBoolean("edit", false);
		
		if(!edit){   // 编辑模式 不需要查询评论
			if(blog0.getInt("user_id").intValue() != this.getCurrUser().getInt("id").intValue()){  // 更细博客查看数量
				Blog.dao.updateViewCount(blog0);
			}
			
			result.setData("comments", Blog.dao.getComments(blogId));
			blog0.set("is_favorite", Blog.dao.isFavorite(blogId, this.getCurrUser().getInt("id")));
			blog0.set("favorite_count", Blog.dao.getFavoriteCount(blogId));
		}
		result.setData("blog", blog0);
		this.renderJson(result.toJson());
	}
	
	public void deleteById(){
		AjaxResult result = new AjaxResult(1);
		int blogId = this.getParaToInt("blog_id");
		Blog.dao.deleteById(blogId);
		Blog.dao.deleteComments(blogId);
		Blog.dao.deleteFavorite(blogId);
		this.renderJson(result.toJson());
	}
	/**
	 * 根据类别查找 博客列表
	 */
	public void findByCateId(){
		AjaxResult result = new AjaxResult(1);
		Record params = this.getParamMap("blog_cate_id", "user_id");
		result.setData("blogs", Blog.dao.findByParams(params));
		this.renderJson(result.toJson());
	}
	public void addOrUpdate(){
		AjaxResult result = new AjaxResult(1,"操作成功");
		
		try {
			Blog blog = this.getModel(Blog.class).setAttrs(this.getParamMap());
			if(StrKit.notNull(blog.get("id"))){
				blog.update();
			}else{
				blog.set("user_id", this.getCurrUser().get("id"));
				blog.set("create_time", DateUtils.getCurrDateTime());
				blog.set("allow_comment", Blog.ALLOW_COMMENT_YES);
				blog.save();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(this.getClass().getName(), e);
			result.setFailure("保存失败");
		}
		this.renderJson(result.toJson());
	}
	
	/***
	 * 上传图片
	 */
	public void upload(){
		AjaxResult result = new AjaxResult(1);
		
		String blogImgPath = Const.BLOG_PATH + File.separator + this.getCurrUser().getStr("user_name");
		String path = PathKit.getWebRootPath() + File.separator + blogImgPath;
		UploadFile uploadFile = this.getFile("imgFile", path);
		String contentType = uploadFile.getContentType();
		if(!contentType.startsWith("image")){
			FileKit.delete(uploadFile.getFile());
			result.setFailure("上传文件必须为图片类型");
		}
		// 重命名图片
		String suffix = FileUtils.getSuffix(uploadFile.getFile());
		String picUrl = blogImgPath + File.separator + UUID.randomUUID().toString().replace("-", "") + suffix;
		String fileName = PathKit.getWebRootPath() + File.separator + picUrl;
		uploadFile.getFile().renameTo(new File(fileName));
		
		result.setData("error", 0);
		result.setData("url", picUrl);
		this.renderJson(result.toJson());
	}
	
}
